#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>//侧装卡口
#include <sys/vfs.h>
#include "tftpser.h"
#include "../VideoQueue/VTInterf.h"
#include "../ad9949/ad9979_type.h"
#include "../ad9949/ad9949.h"
#include "../CARLIB/identifylib.h"
#include "../thmd/version.h"
#include "../comman.h"
#include "../fpga/FPGA_Module.h"
#include "../Param/Param.h"
#include "../udp_report/udp_report.h"
#include "../CommDef/NDC300LHdef.h"
#include "../xml/DataManage.h"
#include "../blend/blend.h"
#include "../main.h"
#include "../fpga/updateDrcTable12t8.h"
#include "../xml/xmlmanage.h"
#include "../PlateListOp.h"
#include "../snmp/stmf.h"
#include "../snmp/security.h"
#include "../oid/oidreg.h"
#include "../ftpcontrol.h"
#include "../DetectStealCard/DetectStealCard.h"
#include "../CARLIB/SideRec.h"
#ifdef UC_MS02
#include "../watchdog/watchdog.h"
#include "getfilethread.h"
#include "../ms_resDeal.h"
#endif // UC_MS02

#define max(a, b)  (((a) > (b)) ? (a) : (b))
#define min(a, b)  (((a) < (b)) ? (a) : (b))
#define TMPBUFLENTH  150
#define MSGBUFLENTH	 ((10)*(1024))
//////////////////////////////全局变量//////////////////////////////////
NightMode g_nightmode;
SmoothSharpCoe g_smoothSharp;
//int16 coeffs[_MATRIX_ADMAXNUM]= {1228,204,-204,0,-102,1024,-102,0,-102,204,1330,0};//此参数不是初始化参数==>Param_matrix_create

int16 g_tftpcoeffs1[_MATRIX_ADMAXNUM] =  {1228,204,-204,0,-102,1024,-102,0,-102,204,1330,0};
int16 g_tftpcoeffs2[_MATRIX_ADMAXNUM] = {1228,204,-204,0,-102,1024,-102,0,-102,204,1330,0};
int16 g_tftpcoeffs3[_MATRIX_ADMAXNUM] = {1228,204,-204,0,-102,1024,-102,0,-102,204,1330,0};
TftpFile listFile[8];	//wbb 7->8 add reg_db
int g_overInfoShowTime = 0;//动态叠加信息显示时间
//raw mode hcm_add_2013.08.06
uint8 rawmode = 0 ,rawflag = 0 ,lightmode = 0;
int g_set_flash_en = 0;
pthread_t	m_tftp_server_thread_t;		//RTSP线程
int m_exit_tftp_thread = 0;
SImageExParam g_Image_Ex_Param;
char g_dynamic_over_param_bugMsg[256] = {0};
int8 mMsgBuf[MSGBUFLENTH] = {0};	//tftp命令码解析缓冲区 大小为10K
int g_saveFontSize = 0;
////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////外部变量//////////////////////////////////////
extern SVTInterf g_IdentInterf;  //
extern iden_res m_res[IDEN_RES_COUNT];	//抓拍结果缓冲区
extern STransParam	g_NetTransParam;	//网络 被动下载 主动上传参数控制
extern int m_EncodeType;
extern int32 m_SCurShutter;	//当前快门速度
extern float m_SCurGain;		//当前增益
extern char g_devversion[30];
extern char *g_resultPicBuf;
extern StJPEGFile  g_JPGFile; 	//大图片缓冲区
extern int g_resPicLen;
extern int g_runStat;
extern int g_feathermode;
extern int32 g_updatafpgaflag;
extern s_dynamic_over_param g_dynamic_over_param;
extern int g_alarm_time;//参数下发的报警持续时间
extern int g_alarmTime_count;//声光报警时间计数
extern int m_rawmode ;
extern s_EncodeResolution 	g_enocodeResolution;
extern s_resolution 		g_resolution;//全局分辨率结构
extern SImageParam		g_ImageParam; //图像参数
extern int BarrierControl;//拦杆机抬杆 1 抬杆
extern blendInfo blend_info;
extern SSystemParam	g_SystemParam;//相机配置参数 300L——H中只用来ETON扫描
extern s_dynamic_VideoOverInfo g_dynamic_VideoOverInfo;
extern SUDPDebug		g_UDPDebugParam;
////////////////////////////////////////////////////////////////////////////////
int TftpInit()
{
	if(tftp_init() != 0)
		return 0;

	listFile[0].fileMaxlen = 1024*1500;
	listFile[1].fileMaxlen = 2048;
	listFile[2].fileMaxlen = 1024*1500;
	listFile[3].fileMaxlen = 1024*1024 * 2;
	listFile[4].fileMaxlen = 1024*10;
	listFile[5].fileMaxlen = 1024*1024*2;
	listFile[6].fileMaxlen = 1024*1024*2;

#ifdef UC_MS02
	listFile[7].fileMaxlen = 1024*1024*2;

	listFile[0].lpFile = (int8 *)malloc((1500 + 2 + 1500+1024*2+10+1024*2+1024*2+2*1024) *1024);
#else
	listFile[0].lpFile = (int8 *)malloc((1500 + 2 + 1500+1024*2+10+1024*2+1024*2) *1024);
#endif // UC_MS02

	if(listFile[0].lpFile == NULL)
	{
		printf("listFile failed\n");
		return 0;
	}
	listFile[1].lpFile = listFile[0].lpFile + 1024*1500;
	listFile[2].lpFile = listFile[1].lpFile + 2048;
	listFile[3].lpFile = listFile[2].lpFile + 1024*1500;
	listFile[4].lpFile = listFile[3].lpFile + 1024*2*1024;
	listFile[5].lpFile = listFile[4].lpFile + 10*1024;
	listFile[6].lpFile = listFile[5].lpFile + 1024*1024*2;
#ifdef UC_MS02
	listFile[7].lpFile = listFile[6].lpFile + 1024*1024*2;
#endif // UC_MS02

	strcpy(listFile[0].Filename, "reg_set");
	strcpy(listFile[1].Filename, "reg_sset");
	strcpy(listFile[2].Filename, "reg_get");
	strcpy(listFile[3].Filename, "image.jpg");
	strcpy(listFile[4].Filename, "xml");
	strcpy(listFile[5].Filename, "reg_pic");
	strcpy(listFile[6].Filename, "noRecPic");
#ifdef UC_MS02
	strcpy(listFile[7].Filename, "reg_db");
#endif // UC_MS02

	listFile[0].fileCurrent = 0;
	listFile[1].fileCurrent = 0;
	listFile[2].fileCurrent = 0;
	listFile[3].fileCurrent = 0;
	listFile[4].fileCurrent = 0;
	listFile[5].fileCurrent = 0;
	listFile[6].fileCurrent = 0;
#ifdef UC_MS02
	listFile[7].fileCurrent = 0;
#endif // UC_MS02

	listFile[2].fileLen = 2;
	listFile[2].lpFile[0] = 0x08;
	listFile[2].lpFile[1] = 0x00;
	listFile[3].fileLen = 0;
	listFile[4].fileLen = 0;
	listFile[5].fileLen = 0;
	listFile[6].fileLen = 0;
#ifdef UC_MS02
	listFile[7].fileLen = 0;
#endif // UC_MS02

	tftp_addFile(&listFile[0]);
	tftp_addFile(&listFile[1]);
	tftp_addFile(&listFile[2]);
	tftp_addFile(&listFile[3]);
	tftp_addFile(&listFile[4]);
	tftp_addFile(&listFile[5]);
	tftp_addFile(&listFile[6]);
#ifdef UC_MS02
	tftp_addFile(&listFile[7]);
#endif // UC_MS02
	return 1;
}

int freeInit()
{
     free(listFile[0].lpFile);
	 return 0;
}

//返回image.jpg文件长度
int32 NetDeal_IsCapFile(void)
{	
	return 	listFile[3].fileLen;
}

int32 NetDeal_IsNoRecFile(void)
{	
	return 	listFile[6].fileLen;
}
int32 NetDeal_IsRegPicFile(void)
{
	return listFile[5].fileLen;
}

static int Pub_GetSubStr(char *substr, char *str, char ch, int sublen)
{
	int i,len,j,k=0;
	char buf[256]="";
	
	if (sublen > 255)
		sublen = 255;
	len = strlen(str);
	substr[0] = '\0';
	for(i=0;i<len;i++)
	{
		if (str[i] == ch)
		{
			strcpy(substr, buf);
			for(j=0;j<len-i-1;j++)
				str[j] = str[j+i+1];
			str[j] = '\0';
			return 0;
		}
		if (str[i] != 0x09 && str[i] != 0x0d && str[i] != 0x0a)
		{
			buf[k++] = str[i];
			buf[k] = '\0';
			if (i >= sublen)
				return -1;
		}
	}
	return -1;
}

//得到子串，getlast=0 则得到标识前的字符串，为1则得到后面字符
static int16 GetSubStr(int8 *substr, int8 *buf, int8 ch, int32 *len)
{
	int16 i = 0,j;
	int32 curLen;

	curLen = *len;
	for(i=0; i<curLen; i++)
	{
		if(buf[i] == ch)
		{
			memcpy(substr,buf,i);
			substr[i] = '\0';
			for(j=0; j<curLen-i-1; j++)
				buf[j] = buf[i+j+1];
			buf[j] = '\0';
            
			*len = curLen - i - 1;
			return 1;
		}
	}
	if(i >= curLen && curLen > 0) 
	{
		memcpy(substr,buf,i);
		substr[i] = '\0';
		*len = 0;
		return 0;
	}
	return 0;
}
//set raw mode hcm_add_2013.08.06

void setstr0x18(int8 *buf)
{
	char s[100];
	char ch='#';
//	uint8 rawmode = 0 ,rawflag = 0 ,lightmode = 0;
	uint16 handshutter = 0;
	uint8  handgain = 0;
	uint8  rawtime = 0;

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	rawmode = atoi(s); //raw mode 0: close 1:open

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	rawflag = atoi(s); //0: auto 1:hand

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	lightmode = atoi(s); //0: auto 1:hand

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	handshutter = atoi(s);

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	handgain = atoi(s);

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	rawtime = atoi(s);

	set_rawmode(rawmode);
	m_rawmode = rawmode;

	if((rawmode == 1) & (rawflag == 1) & (lightmode == 1))
	{
		setShutter(handshutter);
		setADGain((float)handgain);
		printf("raw mode set shutter:%d,gain:%d\n",handshutter,handgain);
	}


}
//raw mode hand zhuapai  hcm_add_2013.08.07
void setstr0x19(int8 *buf)
{
	set_rawcap();
}
//raw mode hand zhuapai  hcm_add_2013.08.07
void readstr0x19(int8 *buf)
{
	int16 index = 0;
	int8 tmpBuf[TMPBUFLENTH];
	uint32 temp;

	temp = raw_cap_sta();//1 :over 0 : not over
	memset(tmpBuf, 0, TMPBUFLENTH);
	index = sprintf(tmpBuf,"%d#",temp);
	memcpy(buf, tmpBuf, index);
	buf[index] = '\0';
	printf("++++++++read ram mode :%d++++++++\n",raw_cap_sta());
}
//时间
void readstr0x15(int8 *buf)//void readstr0x06(int8 *buf)//lyf edit
{
	int16 index = 0;
	int8 tmpBuf[TMPBUFLENTH];
	
			printf("read params 0x15\n");
	memset(tmpBuf, 0, TMPBUFLENTH);
	index = sprintf(tmpBuf, "%d#%d#%d#%d#",
					1, 2, 3, 4);
	memcpy(buf, tmpBuf, index);
	buf[index] = '\0';
}
/*int16 coeffs1[9] = {2048,410,0,0,2048,0,0,1024,2048};//此参数不是初始化参数==>Param_matrix_create
uint8 WB_Switch = 30;

void readstr0xf6(int8 *buf)
{
	int16 index = 0;
	int8 tmpBuf[TMPBUFLENTH];
	
	memset(tmpBuf, 0, TMPBUFLENTH);
	index = sprintf(tmpBuf,"%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#%d#",g_smoothSharp.th1, g_smoothSharp.th2, g_smoothSharp.th3,\
	                 g_smoothSharp.uvth1, g_smoothSharp.threshold, coeffs[0], coeffs[1], coeffs[2], coeffs[3], coeffs[4], coeffs[5],\
	                 coeffs[6], coeffs[7], coeffs[8], coeffs1[0], coeffs1[1], coeffs1[2], coeffs1[3], coeffs1[4], coeffs1[5],\
	                 coeffs1[6], coeffs1[7], coeffs1[8],WB_Switch);
	memcpy(buf, tmpBuf, index);
	buf[index] = '\0';
}
*/
//read current light
/*
extern int16 g_averLight[4];

void read_aver_light(int8 *buf)
{
	int16 index = 0;
	int8 tmpBuf[TMPBUFLENTH];
	uint32 temp;

	temp = (g_averLight[0]*306 + g_averLight[1]*602 + g_averLight[2]*116)>>10;
	memset(tmpBuf, 0, TMPBUFLENTH);
	index = sprintf(tmpBuf,"%d#",temp);
	memcpy(buf, tmpBuf, index);
	buf[index] = '\0';
}
*/
//夜间模式参数
void readstr0x44(int8 *buf)
{
	int16 index = 0;
	int8 tmpBuf[TMPBUFLENTH];
	
	memset(tmpBuf, 0, TMPBUFLENTH);
	index = sprintf(tmpBuf,"%d#%d#%d#%d#%d#",g_nightmode.NightMaxPlusNum,
		g_nightmode.NightAutoWb,g_nightmode.Red_Color,g_nightmode.Green_Color,g_nightmode.Bule_Color);
	memcpy(buf, tmpBuf, index);
	//index += strlen(tmpBuf);
	//buf[index++] = 0x3b;
	buf[index] = '\0';
}
/*lxfun 2013-11-15
函数名称：   setstr0x1e
函数功能：   设置拉伸分界点/小波降噪阈值/高亮溢出阈值/动态压缩点数阈值/过曝点数百分比
                拉伸分界点+小波降噪阈值+高亮溢出阈值+动态压缩点数阈值+过曝点数百分比
                        拉伸分界点：0~1023
                        小波降噪阈值：0~255
                        高亮溢出阈值：0~16383
                        动态压缩点数阈值：0~100
                        过曝点数百分比：0~100
参数：             未用
返回值：            未用
*/
//拉伸分界点+小波降噪阈值+高亮溢出阈值+动态压缩点数阈值+过曝点数百分比
int g_extend_minmult = 1; //扩展模块最小倍数
int g_extend_maxnums = 0; //扩展模块最大点数
int g_extend_maxmult = 1; //扩展模块最大倍数
int g_extend_aveval  = 4095; //扩展模块5*5平均阈值
uint32 g_stretchSplit = 512, g_noiseThreshold = 15, g_dynEncThreshold = 50, g_overExpPercent = 8;
//static uint8 g_yee_th1 = 255;
//static uint8 g_yee_th2 = 0;
//static uint8 g_dnf_wei1 = 1;
//static uint8 g_dnf_wei2 = 4;
void setYeeTh1(uint16 th){g_Image_Ex_Param.yee_th1 = th;}
void setYeeTh2(uint16 th){g_Image_Ex_Param.yee_th2 = th;}
uint16 getYeeTh1(){return g_Image_Ex_Param.yee_th1;}
uint16 getYeeTh2(){return g_Image_Ex_Param.yee_th2;}
void setDnfWei1(uint16 th){g_Image_Ex_Param.dnf_wei1 = th;}
void setDnfWei2(uint16 th){g_Image_Ex_Param.dnf_wei2 = th;}
uint16 getDnfWei1(){return g_Image_Ex_Param.dnf_wei1;}
uint16 getDnfWei2(){return g_Image_Ex_Param.dnf_wei2;}
void setstr0x1e(int8 *buf)
{
	char s[100];
	char ch='#';
	int temp;

			//拉伸分界点
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
			return;
	//g_stretchSplit = atoi(s);
	//memset(data, 0x00, sizeof(data));
	//sprintf(data,"%d", g_stretchSplit);
	//printf("lxprintf 2014-3-28 setstr0x1e g_stretchSplit=%d\r\n", g_stretchSplit);
	//Param_set(_PARA_PICPROCESS, _PICPROC_STRETCHSPLIT, data);
	//小波降噪阈值
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
			return;
	//g_noiseThreshold = atoi(s);
	//          sprintf(data,"%d", g_noiseThreshold);
	//          printf("lxprintf 2014-3-28 setstr0x1e g_noiseThreshold=%d\r\n", g_noiseThreshold);
			//Param_set(_PARA_PICPROCESS, _PICPROC_NOISETHRESHOLD, data);
	//高亮溢出
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
			return;
	g_Image_Ex_Param.HIGH = atoi(s);
	printf("lxprintf 2014-3-28 setstr0x1e g_Image_Ex_Param.HIGH=%d\r\n", g_Image_Ex_Param.HIGH);
	set_Rb_over(g_Image_Ex_Param.HIGH);

	//动态压缩点数阈值
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
			return;
	//过暴点数百分比
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
			return;

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	//扩展模块最小倍数
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	//扩展模块最大倍数
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	//扩展模块5*5平均阈值
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	//扩展模块Gamma
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	g_Image_Ex_Param.extend_gamma = atoi(s);
	printf("lxprintf 2014-3-28 setstr0x1e g_Image_Ex_Param.extend_gamma=%d\r\n", g_Image_Ex_Param.extend_gamma);
	//扩展模块最大点数
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	//DEL NOISE
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	g_Image_Ex_Param.extend_noise = atoi(s);
	FPGA_DEL_NOISE(g_Image_Ex_Param.extend_noise);
	//DEL NOISE1
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;

	//color kill
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	g_Image_Ex_Param.colorkill = atoi(s);
	FPGA_Set_colorkill_th(g_Image_Ex_Param.colorkill);
	//printf("set color kill111111\r\n");

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	//printf("set color kill222222\r\n");
	g_Image_Ex_Param.contrast = atoi(s);//对比度,已经扩展了10倍,范围1.0-3.0

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		  return;
	// -----YEE TH1----
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	temp = atoi(s);
	g_Image_Ex_Param.yee_th1 = temp;
	FPGA_SetYeeTh1(temp);

	// -----YEE TH2----
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	temp = atoi(s);
	g_Image_Ex_Param.yee_th2 = temp;
	FPGA_SetYeeTh2(temp);

	// -----DNF weight1----
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	temp = atoi(s);
	g_Image_Ex_Param.dnf_wei1 = temp;

	// -----DNF weight2----
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	temp = atoi(s);
	g_Image_Ex_Param.dnf_wei2 = temp;

	FPGA_SetDnfWeight(g_Image_Ex_Param.dnf_wei1,g_Image_Ex_Param.dnf_wei2);
	Param_image_Ex_Set(1);
	fpga_drcSetgammaflag();
}
//test shutter
void setstr0x12(int8 *buf)
{
	char s[100];
	char ch='#';
	int sValue,gValue;

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	sValue = atoi(s);
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	gValue = atoi(s);
	m_SCurShutter = sValue;
	m_SCurGain = gValue / 100;
	setShutter(m_SCurShutter);
	setADGain(m_SCurGain);
	printf("set shutter & gain:%d,g:%d\n",sValue,gValue);

}
void setstr0x44(int8 *buf)
{
	char s[100];
	char ch='#';
	uint16 rValue,gValue,bValue;

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	rValue = atoi(s);
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	gValue = atoi(s);
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	bValue = atoi(s);

	setRGBPxga(rValue,gValue,bValue);
	printf("set colour r:%d,g:%d,b:%d\n",rValue,gValue,bValue);

}

void setstr0xf5(int8 *buf)
{
	char s[100];
	char ch='#';
	uint32 temp;
	char tmp[30];
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	temp = atoi(s);
	set_Rb_over(temp);
	sprintf(tmp, "%d", temp);
	Param_set(_PARA_CAP, _PARA_HIGH , tmp);
	printf("%d\n",temp);
}
/*
void SetColorMat(int8 *buf)
{
	char s[100];
	char ch='#';
	int i = 0;
	for(i = 0;i < 9;i ++)
	{
		coeffs[i] = 0;
		if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
			return;
		coeffs[i] = atoi(s);
	}
	FPGA_SetColorMat(&coeffs[0]);
}
*/
// Set Night AWB
void SetNight_AWB(int8 *buf)
{
#ifdef NVD200
	char s[100];
	char ch='#';
	uint8 temp = 0;
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	temp = atoi(s);
	FPGA_SetNight_AWB(temp);
	printf("【Night AWB：%d】\n",temp);
#endif
}

void SetColorMat_Day(int8 *buf)
{
        char s[100];
        char data[50];
        char ch='#';
        int16 value[12];
        uint32 i = 0;

    	char paramName[_MATRIX_ADMAXNUM][32] = {
    			_MATRIX_AD1A11,
    			_MATRIX_AD1A12,
    			_MATRIX_AD1A13,
    			_MATRIX_AD1A14,
    			_MATRIX_AD1A21,
    			_MATRIX_AD1A22,
    			_MATRIX_AD1A23,
    			_MATRIX_AD1A24,
    			_MATRIX_AD1A31,
    			_MATRIX_AD1A32,
    			_MATRIX_AD1A33,
    			_MATRIX_AD1A34
    	};
        for(i = 0;i < _MATRIX_ADMAXNUM;i ++)
        {
			value[i] = 0;
			if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
					return;
			g_tftpcoeffs1[i] = atoi(s);
        }
        for(i = 0; i < _MATRIX_ADMAXNUM; i++)
        {
            memset(data, 0x00, sizeof(data));
            sprintf(data,"%d",g_tftpcoeffs1[i]);
            Param_set(_PARA_MATRIX1, paramName[i], data); //
        }

        FPGA_SetColorMat(&g_tftpcoeffs1[0]);
}

//set night colour mat
void SetColorMat_Night(int8 *buf)
{
        char s[100];
        char data[50];
        char ch='#';
        uint32 i = 0;
        char paramName[_MATRIX_ADMAXNUM][32] = {
        			_MATRIX_AD2A11,
        			_MATRIX_AD2A12,
        			_MATRIX_AD2A13,
        			_MATRIX_AD2A14,
        			_MATRIX_AD2A21,
        			_MATRIX_AD2A22,
        			_MATRIX_AD2A23,
        			_MATRIX_AD2A24,
        			_MATRIX_AD2A31,
        			_MATRIX_AD2A32,
        			_MATRIX_AD2A33,
        			_MATRIX_AD2A34
        	};

        for(i=0;i<_MATRIX_ADMAXNUM;i++)
        {
			if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
					return;
			g_tftpcoeffs2[i] = atoi(s);
        }

        for(i = 0; i < _MATRIX_ADMAXNUM; i++)
        {
            memset(data, 0x00, sizeof(data));
            sprintf(data,"%d",g_tftpcoeffs2[i]);
            Param_set(_PARA_MATRIX2, paramName[i], data);
        }

        FPGA_SetColorMat(&g_tftpcoeffs2[0]);
}

//set flash colour mat
void SetColorMat_Flash(int8 *buf)
{
        char s[100];
        char data[50];
        char ch='#';
        uint32 i = 0;
        char paramName[_MATRIX_ADMAXNUM][32] = {
    			_MATRIX_AD3A11,
    			_MATRIX_AD3A12,
    			_MATRIX_AD3A13,
    			_MATRIX_AD3A14,
    			_MATRIX_AD3A21,
    			_MATRIX_AD3A22,
    			_MATRIX_AD3A23,
    			_MATRIX_AD3A24,
    			_MATRIX_AD3A31,
    			_MATRIX_AD3A32,
    			_MATRIX_AD3A33,
    			_MATRIX_AD3A34
    	};

        for(i=0;i<_MATRIX_ADMAXNUM;i++)
        {
			if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
				return;
			g_tftpcoeffs3[i] = atoi(s);
        }
        for(i = 0; i < _MATRIX_ADMAXNUM; i++)
        {
            memset(data, 0x00, sizeof(data));
            sprintf(data,"%d",g_tftpcoeffs3[i]);
            Param_set(_PARA_MATRIX3, paramName[i], data);
        }

        FPGA_Flash_mode(1); //设置闪光模式 0-不是，1-是
        FPGA_SetColorMat(&g_tftpcoeffs3[0]);
        FPGA_Flash_mode(0);
}
/*
//set night colour mat
void SetColorMat1(int8 *buf)
{
	char s[100];
	char ch='#';
	int8 i = 0;

	for(i=0;i<9;i++)
	{
		coeffs1[i] = 0;
		if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
			return;
		coeffs1[i] = atoi(s);
		coeffs[i] = atoi(s);
	}

	FPGA_SetColorMat(&coeffs1[0]);
	FPGA_SetColorMat(&coeffs[0]);
}
*/

/*
//light & night--- yuzhi
void SetDayNight_TH(int8 *buf)
{
	char s[100];
	char ch='#';
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	WB_Switch = atoi(s);
	printf("【夜间关闭白平衡阈值：%d】\n",WB_Switch);
}
*/

/*lxfun 2014-3-10
函数名称：	checkInfoValid
函数功能：	叠加信息范围检测,详细范围说明见NDC300L基础动态库SDK.doc
参数：		叠加信息指针
返回值：		0－无效 1－有效
*/
int checkInfoValid(s_dynamic_over_info *pInfo)
{
	int i = 0;
	if(pInfo->m_iDynamicOverMessNum < 1 || pInfo->m_iDynamicOverMessNum > MAX_OVERMESSNUM)
	{
		//StringToHex("动态叠加信息个数设置错误", g_dynamic_over_param_bugMsg);
		strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd3d0c5cfa2b8f6cafdc9e8d6c3b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成
		return 0;
	}

	if(pInfo->m_iDynamicOverStand < 0 || pInfo->m_iDynamicOverStand > MAX_DYNOVERSTANDTIME)
	{
		//StringToHex("动态叠加持续时间设置错误", g_dynamic_over_param_bugMsg);
		strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd3b3d6d0f8cab1bce4c9e8d6c3b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

		return 0;
	}

	for(i = 0; i < pInfo->m_iDynamicOverMessNum; i++)
	{
		if(pInfo->m_pOver_mess[i].iBackColor != 0)
		{
			//StringToHex("动态叠加 底色 设置错误", g_dynamic_over_param_bugMsg);
			strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320b5d7c9ab20c9e8d6c3b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

			return 0;
		}

		if(pInfo->m_pOver_mess[i].iFont < 1 || pInfo->m_pOver_mess[i].iFont > 5)
		{
			//StringToHex("动态叠加 字体 设置错误", g_dynamic_over_param_bugMsg);
			strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320d7d6cce520c9e8d6c3b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

			return 0;
		}

		if(pInfo->m_pOver_mess[i].iFontSize > FONT_MAX_SIZE)
		{
			//StringToHex("动态叠加 字号 设置错误", g_dynamic_over_param_bugMsg);
			strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320d7d6bac520c9e8d6c3b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

			return 0;
		}

		if(pInfo->m_pOver_mess[i].iEdgeColor != 0)
		{
			//StringToHex("动态叠加 字符边框颜色 设置错误", g_dynamic_over_param_bugMsg);
			strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320d7d6b7fbb1dfbff2d1d5c9ab20c9e8d6c3b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

			return 0;
		}

		if(pInfo->m_pOver_mess[i].iCharColor != 0)
		{
			//StringToHex("动态叠加 字符颜色 设置错误", g_dynamic_over_param_bugMsg);
			strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320d7d6b7fbd1d5c9ab20c9e8d6c3b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

			return 0;
		}

		if(pInfo->m_pOver_mess[i].iSpace != 0)
		{
			//StringToHex("动态叠加 字符间隔 设置错误", g_dynamic_over_param_bugMsg);
			strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320d7d6b7fbbce4b8f420c9e8d6c3b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

			return 0;
		}
		if(strlen(pInfo->m_pOver_mess[i].szText) == 0 || strlen(pInfo->m_pOver_mess[i].szText) > MAX_OVERCHARNUM - 1)
		{
			//StringToHex("动态叠加 字符串长度错误", g_dynamic_over_param_bugMsg);
			strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320d7d6b7fbb4aeb3a4b6c8b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

			return 0;
		}

		if(pInfo->m_pOver_mess[i].iLeftDis + strlen(pInfo->m_pOver_mess[i].szText) * ZK_WIDTH/2 * pInfo->m_pOver_mess[i].iFontSize > g_enocodeResolution.EncodeWide)
		{
			//StringToHex("动态叠加 字符串水平坐标越界", g_dynamic_over_param_bugMsg);
			strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320d7d6b7fbb4aecbaec6bdd7f8b1ead4bdbde7");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

			return 0;
		}

		if(pInfo->m_pOver_mess[i].iTopDis + ZK_HEIGHT * pInfo->m_pOver_mess[i].iFontSize > g_enocodeResolution.EncodeHeight)
		{
			//StringToHex("动态叠加 字符串竖直坐标越界", g_dynamic_over_param_bugMsg);
			strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320d7d6b7fbb4aecafad6b1d7f8b1ead4bdbde7");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

			return 0;
		}
	}

	if(pInfo->m_AlertInfo.iAlertFlag != 1 && pInfo->m_AlertInfo.iAlertFlag != 0)
	{
		//StringToHex("动态叠加 报警状态设置错误", g_dynamic_over_param_bugMsg);
		strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320b1a8beafd7b4ccacc9e8d6c3b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

		return 0;
	}

	if(pInfo->m_AlertInfo.iAlertTime < 0 || pInfo->m_AlertInfo.iAlertTime > MAXALARMLASTTIME)
	{
		//StringToHex("动态叠加 报警FONT_MAX_SIZE持续时间错误", g_dynamic_over_param_bugMsg);
		strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320b1a8beafb3d6d0f8cab1bce4b4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

		return 0;
	}
/*
	if(pInfo->m_VideoOverInfo.VideoOverFlag < 0 || pInfo->m_VideoOverInfo.VideoOverFlag > 1)
	{
		//StringToHex("动态叠加 视频是否叠加时间标志错误", g_dynamic_over_param_bugMsg);
		strcpy(g_dynamic_over_param_bugMsg, "b6afccacb5febcd320cad3c6b5cac7b7f1b5febcd3cab1bce4b1ead6beb4edcef3");//此字符串在WIN环境下通过StringToHex("XX", g_dynamic_over_param_bugMsg);生成

		return 0;
	}
*/
	return 1;
}

/*lxfun 2014-3-10
函数名称：	SetOverInfo
函数功能：	设置动态叠加信息
参数：
			buf[in] : 参数所在缓冲区
			bufLen[in] : 参数缓冲区长度
返回值：		1 成功 0 失败
*/
int8	SetOverInfo(char *buf,int32 bufLen)
{
	int ret = 0;
	char temp[MAX_OVERCHARNUM]={0};

	if(Xml_JieXiOverParamXml(buf,bufLen,&(g_dynamic_over_param.dynamicInfo)) == 0)
	{
		printf("lxprintf 2014-3-10 SetOverInfo set system param fail\r\n");
		return 0;
	}

	if(g_dynamic_over_param.dynamicInfo.iBanisterFlag != 0)
	{//栏杆机状态命令，检测防盗卡
		fprintf(stderr,"lxprintf 2014-12-24 SetOverInfo stealCard\r\n");
		fprintf(stderr,"lxprintf 2015-1-4 SetOverInfo g_dynamic_over_param.dynamicInfo.iBanisterFlag=%d\r\n", g_dynamic_over_param.dynamicInfo.iBanisterFlag);
		SetBanisterFlag(g_dynamic_over_param.dynamicInfo.iBanisterFlag);
	}
	else
	{
		fprintf(stderr,"lxprintf 2014-12-24 SetOverInfo 动态叠加功能\r\n");
		ret = checkInfoValid(&(g_dynamic_over_param.dynamicInfo));
		if(ret == 0)
		{
			printf("lxprintf 2014-3-10 SetOverInfo param error\r\n");
			memset(&(g_dynamic_over_param.dynamicInfo), 0, sizeof(s_dynamic_over_info));
			g_dynamic_over_param.dynamicInfo.m_iDynamicOverMessNum = 1;
			g_dynamic_over_param.dynamicInfo.m_iDynamicOverStand = 5;
			g_dynamic_over_param.dynamicInfo.m_pOver_mess[0].iBackColor = 0;
			g_dynamic_over_param.dynamicInfo.m_pOver_mess[0].iCharColor = 0;
			g_dynamic_over_param.dynamicInfo.m_pOver_mess[0].iEdgeColor = 0;
			g_dynamic_over_param.dynamicInfo.m_pOver_mess[0].iFont = 3;
			g_dynamic_over_param.dynamicInfo.m_pOver_mess[0].iFontSize = 5;
			g_dynamic_over_param.dynamicInfo.m_pOver_mess[0].iLeftDis = 0;
			g_dynamic_over_param.dynamicInfo.m_pOver_mess[0].iSpace = 0;
			g_dynamic_over_param.dynamicInfo.m_pOver_mess[0].iTopDis = 200;
	//		g_dynamic_over_param.dynamicInfo.m_VideoOverInfo.VideoOverFlag = 1;

			HexToString(g_dynamic_over_param_bugMsg, temp);
			memset(g_dynamic_over_param_bugMsg, 0, sizeof(g_dynamic_over_param_bugMsg));
			strcpy(g_dynamic_over_param.dynamicInfo.m_pOver_mess[0].szText, temp);
			g_alarmTime_count = 0;
		}
		blend_info.m_blend_mess.iSize = g_dynamic_over_param.dynamicInfo.m_pOver_mess[0].iFontSize;
		if( g_saveFontSize != blend_info.m_blend_mess.iSize)
		{
			g_saveFontSize = blend_info.m_blend_mess.iSize;
			set_BlendResetVal(1);
		}

	//	blend_time(g_dynamic_over_param.dynamicInfo.m_VideoOverInfo.VideoOverFlag);

		//动态叠加下发报警参数
		if(g_dynamic_over_param.dynamicInfo.m_AlertInfo.iAlertFlag == 0)
		{
			g_alarmTime_count = 0;
		}
		else
		{
			if(g_dynamic_over_param.dynamicInfo.m_AlertInfo.iAlertTime == 0)
				g_alarmTime_count = g_alarm_time;
			else
				g_alarmTime_count = g_dynamic_over_param.dynamicInfo.m_AlertInfo.iAlertTime;
		}
		g_overInfoShowTime = g_dynamic_over_param.dynamicInfo.m_iDynamicOverStand;

		overInfo_init_no_vdce(1);

	}
	return 1;
}

/*lxfun 2014-3-10
函数名称：	SetDateTimeShow
函数功能：	设置H264视频显示控制
参数：
			buf[in] : 参数所在缓冲区
			bufLen[in] : 参数缓冲区长度
返回值：		1 成功 0 失败
*/
int8	SetDateTimeShow(char *buf,int32 bufLen)
{
	if(Xml_JieXiVideoOverTimeParamXml(buf,bufLen,&(g_dynamic_VideoOverInfo)) == 0)
	{
		printf("lxprintf 2014-3-10 SetDateTimeShow set system param fail\r\n");
		return 0;
	}

	if(g_dynamic_VideoOverInfo.VideoOverFlag >1 || g_dynamic_VideoOverInfo.VideoOverFlag < 0)
		return 0;

	blend_time(g_dynamic_VideoOverInfo.VideoOverFlag);
	return 1;
}

//功能:	设置图像参数
//参数:	buf		[in]:参数所在的缓冲区
//		bufLen	[in]:参数的长度
//返回:	1成功 0失败
int8	SetImageParam(char *buf,int32 bufLen)
{
	//把XML中的图像参数解析到ImageParam中
	if(Xml_JieXiImageParamXml(buf,bufLen,&g_ImageParam) == 0)
	{
		return 0;
	}

	return 1;
}

//功能:	下载系统配置参数
//参数:	response	[out] 参数请求回应的数据缓冲区
//		resplen		[in] 回应数据缓冲区最大长度
//返回:	0:失败 1:成功
int32	DownTheSystemConfig(int8 *buf,int32 *size)
{
	int ret = 0;
	struct timeval tv;
	struct timezone tz;
	struct tm *gtm, timeSc;

	ret = gettimeofday(&tv,&tz);
	if(ret == -1)
	{
		perror("[Function] DownTheSystemConfig");
	}
	tv.tv_sec +=  8*60*60;	//获取的是GMT时间 转为本地时间
	gmtime_r(&(tv.tv_sec), &timeSc);
	gtm = &timeSc;

	g_SystemParam.curTime.year	= (gtm->tm_year + 1900);
	g_SystemParam.curTime.month	= gtm->tm_mon + 1;
	g_SystemParam.curTime.day		= gtm->tm_mday;
	g_SystemParam.curTime.hour	= gtm->tm_hour;
	g_SystemParam.curTime.minute	= gtm->tm_min;
	g_SystemParam.curTime.second	= gtm->tm_sec;

	g_SystemParam.UDPUpPort = g_NetTransParam.udpPort;

	//生成XML格式的参数
	if(Xml_MakeSystemConfigString(&g_SystemParam,buf, size) == 0)
	{
		return 0;
	}

	return 1;
}


//功能：处理TFTP参数读取请求，并准备请求的回应数据
//参数：cmd 	[in]	- 输入的参数请求命令
//		cmdlen	[in]	- 参数请求命令长度
//		response[out]	- 参数请求回应的数据缓冲区
//		resplen	[in]	- 回应数据缓冲区最大长度
//返回：回应数据有效长度 <=0 表示异常
int32 MsgRequst(int8 *cmd, int32 cmdlen, int8 *response, int32 resplen)
{
	s_side_param tmpSideParam;

	int32	ret = 0;

	int8 *shareMem = response+32;//保留头部的32字节
	int32 shareMemLen = resplen-32-1;//减去头部数据和结尾的校验字节
	int32 size = shareMemLen;
	char xmlHeadFlag[32];
	struct statfs stat;
	int64 DiskSize;

	//将头部设为空字符串 在函数退出时检查 如果不为空则表示缓冲区
	//有数据,则要加上头部和尾部
	xmlHeadFlag[0] = '\0';
	Set_UserOperaLastTime();//记录最后操作时间

	//系统配置参数,用于ETON设备扫描
	if(strcmp(cmd, MSG_SYSTEMCFG) == 0)
	{

		ret = DownTheSystemConfig(shareMem, &size);
		if(ret <= 0)			//成功
		{
			return 0;
		}
		strcpy(xmlHeadFlag,MSG_SYSTEMCFG);
	}

	//图像参数
	if(strcmp(cmd, MSG_IMAGECFG) == 0)
	{
		//生成XML格式的图像参数
		if(Xml_MakeImageParamString(&g_ImageParam, shareMem, &size) == 0)
		{
			return 0;
		}
		strcpy(xmlHeadFlag,MSG_IMAGECFG);
	}

	//侧装卡口参数
	if(strcmp(cmd, MSG_SIDEPARAM) == 0)
	{
		//生成XML格式的图像参数
		GetSideParam(&tmpSideParam);
		statfs("/mnt", &stat);
		DiskSize= (int64)((int64)stat.f_bfree * (int64)stat.f_bsize /1024 /1024);//MB
		if(DiskSize - 1024 >= 0)
		{
			tmpSideParam.saveSpace = DiskSize - 1024;
		}
		else
		{
			tmpSideParam.saveSpace = 0;
		}

		if(Xml_MakeSideParamString(&tmpSideParam, shareMem, &size) == 0)
		{
			return 0;
		}
		strcpy(xmlHeadFlag,MSG_SIDEPARAM);
	}

	//调试信息参数
	if(strcmp(cmd, MSG_DEBUGPARAM) == 0)
	{
		if(Xml_MakeDebugParamString(&g_UDPDebugParam, shareMem, &size) == 0)
		{
			return 0;
		}
		strcpy(xmlHeadFlag,MSG_DEBUGPARAM);
	}

	if(xmlHeadFlag[0]!='\0')
	{
		//新的打包方式
		if(Xml_MakeMessage1(xmlHeadFlag, (uint8 *)response, &size,resplen) == 0)
		{
			return 0;
		}
		return size;
	}
	return 0;
}

//功能：TFTP设置命令
//参数：cmd 		[in]	- 输入的参数请求命令
//		cmdlen		[in]	- 参数请求命令长度
//		respBuf		[in]	- 存放响应数据的缓冲区
//		respBufLen [in/out] - in:响应缓冲区的长度 out:其中数据的长度
//返回：0 - 非法命令(兼容原有协议)，1 - 成功，-1 - 失败
int MsgUpdate(int8 *cmd, int32 cmdlen,int8 *respBuf, int32 *bufLen)
{
	uint32	FileLength;
	int8 *shareMem = (int8 *)mMsgBuf;
	int32 shareMemLen = MSGBUFLENTH;

	memset(shareMem, 0, MSGBUFLENTH);
	Set_UserOperaLastTime();//记录最后操作时间

	//1.解包
	FileLength = shareMemLen;
	if(Xml_JieXiMessage(cmd, cmdlen, shareMem, &FileLength) == 0)	//校验失败
	{
		printf("Xml_JieXiMessage failed\n");
		return 0;
	}
	*(shareMem + FileLength) = 0;
	//2.抓拍一张抓拍图片不识别
	if(strcmp(cmd, MSG_GETCAPPIC) == 0)
	{
		g_feathermode = g_feathermode | 0x08;
		printf("MsgUpdate g_feathermode 0x%x\n",g_feathermode);
		return 1;
	}

	//3.设置动态叠加信息
	if(strcmp(cmd, MSG_OVERINFO) == 0)
	{
		if(SetOverInfo(shareMem, FileLength) == 1)
		{
			return 1;
		}
		else
		{
			return -1;
		}
	}

	/* 4.车牌列表 */
	if (strcmp(cmd, MSG_PLATELIST) == 0)
	{
		int ret;
		int bufLenBk = *bufLen - 32; /* 跳过消息头 */
		
		ret = DealPlateXML(shareMem, FileLength, shareMem + FileLength, shareMemLen - FileLength, respBuf + 32, &bufLenBk);
		if (ret == 1)
		{
			//给数据加上头部和校验 目前只有这一个地方需要返回数据,所以加在此 
			//如果有多个的时候,应该抽取出来放在函数末尾
			if(Xml_MakeMessage1(MSG_PLATELIST, (uint8 *)respBuf, &bufLenBk,*bufLen) == 0)
			{
				return -1;
			}
			*bufLen = bufLenBk;
			return bufLenBk;
		}
	}

	//5.图像参数
	if(strcmp(cmd, MSG_IMAGECFG) == 0)
	{
		if(SetImageParam(shareMem, FileLength) == 1)			//设置成功
		{
			Params_FaceBackTolight_Set();

			return 1;
		}
		else								//设置失败
		{
			return -1;
		}
	}
	//6.设置H264视频时间显示
	if(strncmp(cmd, MSG_VIDEOOVERINFO, 12) == 0)//动态库最多下发12字节的命令，但此命令13字节，为了不改动态库，相机做个处理
	{
		if(SetDateTimeShow(shareMem, FileLength) == 1)
		{
			return 1;
		}
		else
		{
			return -1;
		}
	}

	//7.侧装卡口参数
	if(strncmp(cmd, MSG_SIDEPARAM, 12) == 0)//动态库最多下发12字节的命令，但此命令13字节，为了不改动态库，相机做个处理
	{
		if(SetSideParamControl(shareMem, FileLength) == 1)
		{
			Param_Side_Set();
			return 1;
		}
		else
		{
			return -1;
		}
	}

	//8.调试信息参数
	if(strncmp(cmd, MSG_DEBUGPARAM, 12) == 0)//动态库最多下发12字节的命令，但此命令13字节，为了不改动态库，相机做个处理
	{
		if(SetDebugParamControl(shareMem, FileLength) == 1)
		{
			return 1;
		}
		else
		{
			return -1;
		}
	}

	//相机除识别外所有参数
	if(strcmp(cmd, MSG_RECCPU) == 0)
	{
		//生成XML格式的所有参数文件
		if(saveAllParamToFile() <= 0)
			return -1;
	}

	return 0;
}

//segmStatistic
void setstr0x25(int8 *buf)
{
   char s[100];
   char data[50];
   char ch='#';
   uint32 i = 0;

   printf("\n");
   for(i=0;i<25;i++)
    {
		if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
			return;
		g_Image_Ex_Param.segmWeight[i] = atoi(s);
		memset(data, 0x00, sizeof(data));
		sprintf(data,"%d", g_Image_Ex_Param.segmWeight[i]);
		printf(" %d", g_Image_Ex_Param.segmWeight[i]);
	}
	printf("\n");

	Param_image_Ex_Set(0);
	set30x40(g_Image_Ex_Param.segmWeight);
}
//set ad9979
void setstr0x15(int8 *buf)
{
	uint8 rgpos1, rglen1, h1pos1, h1len1, h2pos1, h2len1, shp1, shlen1, doutp1;
	uint8 rgpos2, rglen2, h1pos2, h1len2, h2pos2, h2len2, shp2, shlen2, doutp2;
	uint8 setLeft, setRight;
	char s[100];
	char ch='#';
	char tmp[30];
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	rgpos1 = atoi(s);

	sprintf(tmp, "%d", rgpos1);
	Param_set(_PARA_AD, _AD_RGPOS1, tmp);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	rglen1 = atoi(s);
	
	sprintf(tmp, "%d", rglen1);
	Param_set(_PARA_AD, _AD_RGLEN1, tmp);

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	h1pos1 = atoi(s);

	sprintf(tmp, "%d", h1pos1);
	Param_set(_PARA_AD, _AD_H1POS1, tmp);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	h1len1 = atoi(s);

	sprintf(tmp, "%d", h1len1);
	Param_set(_PARA_AD, _AD_H1LEN1, tmp);	

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	h2pos1 = atoi(s);

	sprintf(tmp, "%d", h2pos1);
	Param_set(_PARA_AD, _AD_H2POS1, tmp);		

	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;

	h2len1 = atoi(s);
	sprintf(tmp, "%d", h2len1);
	Param_set(_PARA_AD, _AD_H2LEN1, tmp);	
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	shp1 = atoi(s);

	sprintf(tmp, "%d", shp1);
	Param_set(_PARA_AD, _AD_SHP1, tmp);	
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	shlen1 = atoi(s);

	sprintf(tmp, "%d", shlen1);
	Param_set(_PARA_AD, _AD_SHLEN1, tmp);	
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	doutp1 = atoi(s);

	sprintf(tmp, "%d", doutp1);
	Param_set(_PARA_AD, _AD_DOUTP1, tmp);		
	//////////////////////////////////////////
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	rgpos2 = atoi(s);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	rglen2 = atoi(s);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	h1pos2 = atoi(s);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	h1len2 = atoi(s);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	h2pos2 = atoi(s);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	h2len2 = atoi(s);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	shp2 = atoi(s);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	shlen2 = atoi(s);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	doutp2 = atoi(s);
	//////////////////////////////////////////
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	setLeft = atoi(s);
	
	if(Pub_GetSubStr(s,buf+1,ch,10) == -1)
		return;
	setRight = atoi(s);

}

//get ad param
uint8 GetAD_param()
{

	Ad9949Check pl;	
	char data[100];

	memset(data, 0x00, sizeof(data));
	Param_get(_PARA_AD, _AD_RGPOS1, data);
	pl.rgpos = atoi(data);

	memset(data, 0x00, sizeof(data));
	Param_get(_PARA_AD, _AD_RGLEN1, data);
	pl.rglen = atoi(data);

	memset(data, 0x00, sizeof(data));
	Param_get(_PARA_AD, _AD_H1POS1, data);
	pl.h1pos = atoi(data);

	memset(data, 0x00, sizeof(data));
	Param_get(_PARA_AD, _AD_H1LEN1, data);
	pl.h1len = atoi(data);

	memset(data, 0x00, sizeof(data));
	Param_get(_PARA_AD, _AD_H2POS1, data);
	pl.h2pos = atoi(data);

	memset(data, 0x00, sizeof(data));
	Param_get(_PARA_AD, _AD_H2LEN1, data);
	pl.h2len = atoi(data);

	memset(data, 0x00, sizeof(data));
	Param_get(_PARA_AD, _AD_SHP1, data);
	pl.shp = atoi(data);

	memset(data, 0x00, sizeof(data));	
	Param_get(_PARA_AD, _AD_SHLEN1, data);
	pl.shlen = atoi(data);

	memset(data, 0x00, sizeof(data));
	Param_get(_PARA_AD, _AD_DOUTP1, data);
	pl.doutp = atoi(data);
	printf("AD:%d,%d,%d,%d,%d,%d,%d,%d,%d\n",pl.rgpos,pl.rglen,pl.h1pos,pl.h1len,pl.h2pos,pl.h2len,pl.shp,pl.shlen,pl.doutp);
	return 0;
}

int16 GetData(int8 *buf)
{
	switch((uint8)buf[0])
	{
		case 0x15:
			readstr0x15(buf);
			break;
		case 0x19:
			readstr0x19(buf);//read raw over  hcm_add_2013.08.07
			break;
		case 0xe4:
//			read_aver_light(buf);
			break;
		case 0x44:
			readstr0x44(buf);
			break;
		case 0xf6:
//			readstr0xf6(buf);
			break;
		default:
			break;
	}
	return 1;
}

static int16 tftp_readparam()  
{
	int8 tmp[1000];
	char ch=';';
	int32 pos = 0;
	int32 len;
	int16 ret = 1;

	len=listFile[1].fileLen;
	//memset(listFile[2].lpFile, 0, 500);
	//while (len > 0)
	if(len > 0) //zhai
	{
		ret = GetSubStr(tmp,listFile[1].lpFile, ch, &len);//以;号来划分
		if(ret == 1)
		{
			//多条命令时，回应命令
			memcpy(listFile[2].lpFile + pos, tmp, 1);
			pos++;
		}
		GetData(tmp);
		memcpy(listFile[2].lpFile + pos, tmp, strlen(tmp));
		pos = pos + strlen(tmp);
		listFile[2].lpFile[pos++] = ';';
		listFile[2].lpFile[pos] ='\0';
		listFile[2].fileLen = pos;
	}
	//memset(listFile[1].lpFile, 0, 500);
	listFile[1].fileLen = 0;
	return 1;
}

static void tftp_setparam()
{
	int32 len;

    len = listFile[0].fileLen;
	switch(listFile[0].lpFile[0])
	{
		//
		case 0x15:
			//printf("------1\n");
			setstr0x15(listFile[0].lpFile);
			break;
		case 0x18:
			setstr0x18(listFile[0].lpFile);//raw mode hcm_add_2013.08.06
			break;
		case 0x19:
			setstr0x19(listFile[0].lpFile);//raw mode hcm_add_2013.08.07
			break;
		case 0xe2:
			//printf("------2\n");
//			setstr0xe2(listFile[0].lpFile);
			break;
		case 0xe3:
			//printf("-----------SetColorMat1\n");
//			SetColorMat1(listFile[0].lpFile);//晚上
			break;
        case 0x25:
            setstr0x25(listFile[0].lpFile);
            break;
        case 0xe6:
			//printf("------3\n");
			SetNight_AWB(listFile[0].lpFile);//Night AWB
		case 0xe4:
			//printf("------4\n");
//			SetDayNight_TH(listFile[0].lpFile);//设置白天晚上阈值
			break;
		case 0xf5:
			//printf("------5\n");
			setstr0xf5(listFile[0].lpFile);
			break;
		case 0x13:
			setFlash_en();
			//g_set_flash_en = 1;
			//resetAD();
			printf("set flash en\n");
			break;
		case 0x1e:
			setstr0x1e(listFile[0].lpFile);
			break;
		case 0x1b: //白天颜色校正矩阵
			SetColorMat_Day(listFile[0].lpFile);
			break;
		case 0x1c: //晚上颜色校正矩阵
			SetColorMat_Night(listFile[0].lpFile);//晚上
			break;
		case 0x1d: //flash颜色校正矩阵
			SetColorMat_Flash(listFile[0].lpFile);
			break;
		case 0x12:
			//printf("------6\n");
			//setShutter(listFile[0].lpFile);
			setstr0x12(listFile[0].lpFile);
			break;
/*		case 0x27://limit
			setstr0x27(listFile[0].lpFile);
			break;*/
		default:
			break;
	}
	listFile[0].lpFile[0] = '\0';
	listFile[0].fileLen = 0;
	return ;
}

#ifdef NDC300L_H_VPB600
static int m_recFlag = 1;
static struct timeval tv_begin;
static struct timeval tv_end;
static struct timezone tz;
static void resPic_Deal()
{
	int addr = -1;
	int mSec = 0;

	addr = CarLibRes_GetFill();
	if(addr == -1)
	{
		return;
	}

	//识别结果处理
	if(NetDeal_IsRegPicFile() <= 0)
	{
		listFile[5].fileLen = 0;
		memcpy(listFile[5].lpFile,m_res[addr].data,m_res[addr].dataLen);
		listFile[5].fileLen = m_res[addr].dataLen;
		m_res[addr].state = 0;
		recognise_report(g_NetTransParam.udpIp,g_NetTransParam.udpPort,m_res[addr].udpStr);
		if(m_recFlag == 1)
		{
			gettimeofday(&tv_begin,&tz);
			m_recFlag = 0;
		}
	}
	else if(g_NetTransParam.overTime > 0)
	{
		gettimeofday(&tv_end,&tz);
		mSec = (tv_end.tv_sec - tv_begin.tv_sec) * 1000 + (tv_end.tv_usec - tv_begin.tv_usec) / 1000;
		if(mSec > g_NetTransParam.overTime)
		{
			//printf("resDeal overtime tftpserver lose one result\n");
			listFile[5].fileLen = 0;
			m_res[addr].state = 0;
			m_recFlag = 1;
		}
	}
	else if(g_NetTransParam.overTime == 0)
	{
		usleep(10);
		listFile[5].fileLen = 0;
		m_res[addr].state = 0;
		m_recFlag = 1;
	}
}

#endif // NDC300L_H_VPB600

#ifdef UC_MS02
//侧装卡口数据TFTP下载准备
void ms_filedeal()
{
	//流量数据库文件上传
	if(ms_NetDeal_IsFluxDBFile()<=0)
	{
		//listFile[7].fileLen = 0;
		//获取交通车辆信息数据库文件
		ms_gettrafficdbfile_tftp();
	}
	//车辆数据文件上传
	if(NetDeal_IsRegPicFile()<=0)
	{
		//获取车辆数据文件放到tftp服务中
		ms_getCarDataIn_tftp();
	}
}
#endif // UC_MS02

void NetDeal_Proc ()
{
	int32 index,ret = 0;
	int32 respBufLen, len;
	int8 *tmp;
	frame_info frame;

	if(TftpInit() == 0)
	{
		return ;
	}
	frame.pData = listFile[3].lpFile;
	StmfInit();
	SecurityInit();
	OID_RegAll(); //reg OID
	printf("**********    Tftp_thread start  *************\n");
 	while(m_exit_tftp_thread != 1)
 	{

 		MainTheaddogSet(2);
 		StmfRun(); //snmp deal
		if(g_updatafpgaflag != 0)
		{
			usleep(20);
			continue;
		}
  	 	//jpg 压缩图片队列上传处理
 		if(NetDeal_IsCapFile() <= 0)
 		{
 			listFile[3].fileLen = 0;
			if(VTInterf_GetData(&g_IdentInterf, &frame) > 0)
			{
				listFile[3].fileLen =  frame.length;
			}
 		}
#ifdef NDC300L_H_VPB600
 		resPic_Deal();	//识别结果处理
#endif // NDC300L_H_VPB600

#ifdef UC_MS02
 		//侧装卡口车辆数据、流量数据下载处理
 		ms_filedeal();
#endif // UC_MS02

		tftp_run();
		tmp = new_put_filename();
/*
		if((tmp != NULL) && (strcmp(tmp,"noRecPic") == 0))
		{
			g_feathermode = g_feathermode & 0xF7;
			listFile[6].fileLen = 0;
			printf("NetDeal_Proc g_feathermode 0x%x,listFile[6].fileLen %d\n",g_feathermode,listFile[6].fileLen);
		}
*/
		tmp = new_get_filename();
		if(tmp == NULL)
			continue;
		index = -1;
		if (strcmp(tmp,"reg_sset") == 0)
			index = 0;
		if (strcmp(tmp,"reg_set") == 0)
			index = 1;
		if(index == 0)
		{
			//判断传输的数据内容
			len = MsgRequst(listFile[1].lpFile, listFile[1].fileLen, listFile[2].lpFile, listFile[2].fileMaxlen);
			if(len > 1)
			{
				listFile[2].fileLen = len;
			}
			else if(len == 0)
			{
				switch(listFile[1].lpFile[0])
				{
				case 0x04:
					strcpy(listFile[2].lpFile,   _MAIN_VERSION);
					strcat(listFile[2].lpFile, "#");
					strcat(listFile[2].lpFile, g_devversion);
					strcat(listFile[2].lpFile, "#");
					listFile[2].fileLen = strlen(listFile[2].lpFile);
					listFile[1].fileLen = 0;
					break;
				case 0x19:
					sprintf(listFile[2].lpFile, "%d#", raw_cap_sta());//
					listFile[2].fileLen = strlen(listFile[2].lpFile);
					listFile[1].fileLen = 0;
					break;
				default:
					break;
				}

				tftp_readparam(); //read
			}
		}

		if(index == 1)
		{
			respBufLen = listFile[2].fileMaxlen;
			ret = MsgUpdate(listFile[0].lpFile, listFile[0].fileLen,listFile[2].lpFile, &respBufLen);
			if (ret > 2)
			{
				listFile[2].fileLen = respBufLen;
			}
			if(ret == 0)
			{
				uint8 tmp_arr[4];
				switch((uint8)listFile[0].lpFile[0])
				{
					case 0x01://标志开始网络抓拍
						g_JPGFile.Caped = 2;
						break;
					case 0x03://设置IO状态
						listFile[0].lpFile[listFile[0].fileLen] = 0;
						//获取IO序号
						tmp_arr[0] = listFile[0].lpFile[1];
						tmp_arr[1] = listFile[0].lpFile[2];
						switch(tmp_arr[0])
						{
						case 0x2:
							BarrierControl = 1;
							break;
						/* case 0x3: */
						/* 	Strobe_Trigger(tmp_arr[1]); */
						/* 	break; */
						default:
							printf("unkown io num\n");
							break;
						}
						break;
				}
			}
			tftp_setparam();	
		}
		usleep(10);
	}

}
int tftp_thread_start()
{
	memset(&m_tftp_server_thread_t, 0, sizeof(m_tftp_server_thread_t));

#ifdef UC_MS02
	//侧装卡口tftp参数初始化
	ms_tftpinit();
#endif // UC_MS02

	//创建线程  开启RTSP 服务
	if((pthread_create(&m_tftp_server_thread_t, NULL, (void *)NetDeal_Proc, NULL)) != 0)
	{
		printf("rtsp_client_thread failed!\n");
		return -1;
	}

#ifdef UC_MS02
	//启动文件查找线程
	getfilethread_start();
#endif // UC_MS02

	return 0;
}
int tftp_thread_exit()
{
	m_exit_tftp_thread = 1;
#ifdef UC_MS02
	//停止文件查找线程
	getfilethread_exit();
	//内存释放
	freeInit();
#endif // UC_MS02

	return 1;
}
